<?php
	class Ads_model extends CI_Model{
		
		public function __construct(){
			$this->load->database();
		}
		
		public function getAdsList($id=FALSE){
			if($id === FALSE){
				$query = $this->db->get('ads');
				return $query->result_array();
			}
			$query = $this->db->get_where('ads', array('id' => $id));
			return $query->row_array();
		}
		
		public function save(){
			$id = $this->input->post('id');
			$data = array(
				"ad_text" => $this->input->post('ad_text')				
			);
			if(empty($id))
				return $this->db->insert('ads', $data);
			else{
				$this->db->where('id', $id);
				return $this->db->update('ads', $data);
			}
		}		
		
		public function delete($id){
			$sql = "SELECT `id` FROM `ads_assign` WHERE `ads_id` = $id";
			$query = $this->db->query($sql);
			if($query->num_rows()==0){
				$this->db->where('id', $id);
				$this->db->delete('ads');
			}
		}
	}
?>